<!-- 系统变量菜单 部门列表组件 -->
<template>
  <div class="">
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator="/">
      <!-- <el-breadcrumb-item><a>用户管理</a></el-breadcrumb-item> -->
      <el-breadcrumb-item>系统变量菜单</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <!-- <el-form :model="queryInfo" ref="queryForm" size="small" :inline="true" label-width="68px">
        <el-form-item label="配置键" prop="paramKey">
          <el-input v-model="queryInfo.paramKey" placeholder="请输入配置键" clearable />
        </el-form-item>
        <el-form-item label="参数" prop="paramIsSys">
          <el-select v-model="queryInfo.paramIsSys" placeholder="是否过滤" clearable style="width: 240px">
            <el-option label="系统参数" value="y">系统参数</el-option>
            <el-option label="业务参数" value="n">业务参数</el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="handleQuery" type="primary" icon="el-icon-search" size="mini">搜索</el-button>
          <el-button @click="resetQuery" icon="el-icon-refresh" size="mini">重置</el-button>
        </el-form-item>
      </el-form> -->

      <el-row :gutter="10" class="mb8" style="marginBottom:20px">
        <el-col :span="1.5"> 
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="addTaskHandle">新增</el-button>
        </el-col>
        <el-col :span="1.5"> </el-col>
        <el-col :span="1.5">
          <!-- <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="deleteTaskHandle">删除</el-button> -->
        </el-col>
        <el-col :span="1.5">
          <!-- <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出</el-button> -->
        </el-col>
      </el-row>

      <el-table :data="userList" tooltip-effect="dark" style="width: 100%" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column prop="paramKey" label="配置键"> </el-table-column>
        <el-table-column prop="paramValue" label="配置值"> </el-table-column>
        <el-table-column prop="paramIsSys" label="参数">
          <template slot-scope="scope">
            <span :value="scope.row.paramIsSys">{{
              scope.row.paramIsSys === 'y' ? '系统参数' : '业务参数'
            }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="说明"> </el-table-column>
        <el-table-column label="操作" align="center" width="220" class-name="small-padding fixed-width">
          <template v-if="scope.row.userId !== 1" slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="updateTaskHandle(scope.row)">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryInfo.currentPage" :page-sizes="[10, 20, 30, 40]" :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>

    <!-- 添加修改用户 弹窗 -->
    <el-dialog :title="upload.title" width="600px"  :visible.sync="dialogVisible" @close="addDialogClosed">
      <!-- 内容主题区域 -->
      <el-form ref="addFormRef" :model="addForm" :rules="addFormRules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="配置键" prop="paramKey">
                <el-input v-model="addForm.paramKey"></el-input>
              </el-form-item>
          </el-col>
            <el-col :span="12">
              <el-form-item label="配置值" prop="paramValue">
                  <el-input v-model="addForm.paramValue"></el-input>
                </el-form-item>
            </el-col>
          <el-col :span="12">
            <el-form-item label="参数" prop="paramIsSys">
              <el-radio-group v-model="addForm.paramIsSys">
                <el-radio label="y">系统参数</el-radio>
                <el-radio label="n">业务参数</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="说明" prop="remark">
              <el-input v-model="addForm.remark"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUser">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  // 组件名称
  name: '',
  // 局部注册的组件
  components: {},
  // 组件参数 接收来自父组件的数据
  props: {},
  // 组件状态值
  data() {
    return {
      UrlOptions: [],
      paramId: undefined,
      // 多选选中的数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      form: {},
      // showSearch: true,
      detail: {}, // 变量详情
      // 获取用户列表数据的参数对象
      queryInfo: {
        // 当前的页数
        currentPage: 1,
        // 当前每页显示多少条数据
        pageSize: 10,
      },
      userList: [],
      total: 0,
      rowData: {},
      // 对话框显示与隐藏
      dialogVisible: false,
      // 是否显示详细弹出层
      openView: false,
      // 表单参数
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: '',
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0
      },
      // 添加用户的表单数据
      addForm: {
        paramIsSys:'y'
      },
         // 表单校验
      addFormRules: {
        paramKey: [
          { required: true, message: "配置键", trigger: "blur" },
        ],
         paramValue: [
          { required: true, message: "配置值", trigger: "blur" },
        ],
        paramIsSys: [
          { required: true, message: "参数", trigger: "blur" },
        ],
      },
    }
  },
  // 计算属性
  computed: {},
  // 侦听器
  watch: {},
  created() {
    this.getList()
  },
  methods: {
    // 列表
    async getList() {
      const params = this.queryInfo
      const { data: res } = await this.$http.post('/sys-param/page', params)
      if (res.code === 20000) {
        this.userList = res.data.records
        this.total = parseInt(res.data.total)
      } else {
        this.$message.error(res.msg)
      }
    },
   
    // 搜索按钮操作
    handleQuery() {
      this.queryInfo.currentPage = 1
      this.getList()
    },
    // 重置按钮操作
    resetQuery() {
      // this.resetForm('queryForm')
      // console.log(this) this指向$refs的是通过ref获得的实例对象loginFormRef
      this.$refs.queryForm.resetFields()
      this.handleQuery()
    },
    // 监听 pageSize 改变的事件
    handleSizeChange(newSize) {
      console.log(newSize)
      this.queryInfo.pageSize = newSize
      this.getList()
    },
    // 监听页码值改变的事件
    handleCurrentChange(newPage) {
      console.log(newPage)
      this.queryInfo.currentPage = newPage
      this.getList()
    },
    // 监听添加用户对话框关闭事件
    addDialogClosed() {
      // 重置表单
      this.$refs['addFormRef'].resetFields()
    },
    // 新增按钮
    addTaskHandle() {
      this.dialogVisible = true
      this.upload.title = '添加变量'
    },
    // 修改按钮
    updateTaskHandle(row) {
      this.dialogVisible = true
      this.upload.title = '修改变量'
      this.addForm = {...row}
      this.paramId = row.paramId
    },
    // 多选按钮 修改
    async updateTasksHandle() {
      this.dialogVisible = true
      this.upload.title = '修改变量'
      this.addForm = this.rowData[0]
    },

    // 提交按钮 添加修改变量
    addUser() {
      this.$refs['addFormRef'].validate(async valid => {
        let arr = []
        if (valid) {
          if (this.paramId !== undefined) {
            const params = this.addForm
            const { data: res } = await this.$http.post(
              '/sys-param/update',
              params
            )
            if (res.code === 20000) {
              this.$message.success(`修改成功!`)
              this.getList()
              this.$refs.addFormRef.resetFields()
              this.dialogVisible = false
              this.paramId = undefined
            } else {
              this.$message.error(res.msg)
            }
          } else {
            const params = this.addForm
            const { data: res } = await this.$http.post(
              '/sys-param/add',
              params
            )
            if (res.code === 20000) {
              this.$message.success(`新增成功!`)
              this.getList()
              this.$refs.addFormRef.resetFields()
              this.dialogVisible = false
              this.paramId = undefined
            } else {
              this.$message.error(res.msg)
            }
          }
        }
      })
    },
     // 删除弹窗封装
    async handleDelete(row) {
      if (!(await this._box(row.paramId, 'get', '/sys-param/remove', 'paramId'))) return;
    },

    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row)
        })
      } else {
        this.$refs.multipleTable.clearSelection()
      }
    },
    toggleAll() {
      this.$refs.multipleTable.toggleAllSelection()
    },
    handleSelectionChange(selection) {
      this.rowData = selection
      this.ids = selection.map(item => item.paramId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    }
  },

  mounted() { }
  // 组件方法
}
</script>

<style scoped lang="less">
// 表格样式
.el-table {
  margin-top: 15px;
  font-size: 12px;
}

.urlSpan {
  display: inline-block;
  width: 100%;
}
</style>
